package be.bonamis.umpire.utils.excel;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.util.IOUtils;

public class Excel2Java {

	private static final Log LOG = LogFactory.getLog(Excel2Java.class);

	public static <T> List<T> getObjectsFromFile(String filename, Map<String, String> cellMapping,
			Class<T> objectClass) {
		OPCPackage pkg = null;
		ExcelWorkSheetHandler<T> workSheetHandler = new ExcelWorkSheetHandler<T>(objectClass,
				cellMapping);
		InputStream in = Excel2Java.class.getClassLoader().getResourceAsStream(filename);
		try {
			pkg = OPCPackage.open(in);
		} catch (InvalidFormatException ife) {
			LOG.error(ife.getMessage(), ife.getCause());
		} catch (IOException ioe) {
			LOG.error(ioe.getMessage(), ioe.getCause());
		} finally {
			IOUtils.closeQuietly(in);
			try {
				if (null != pkg) {
					pkg.close();
				}
			} catch (IOException e) {
				// just ignore IO exception
			}
		}
		ExcelReader excelReader = new ExcelReader(pkg, workSheetHandler);
		excelReader.process();

		if (workSheetHandler.getValueList().isEmpty()) {
			// No data present
			LOG.error("sHandler.getValueList() is empty");
		} else {

			LOG.info(workSheetHandler.getValueList().size()
					+ " no. of records read from given excel worksheet successfully.");

			// Displaying data ead from Excel file
			return workSheetHandler.getValueList();

		}
		return null;
	}

	public static <T> List<T> getObjectsFromByteArray(byte[] file, Map<String, String> cellMapping,
			Class<T> objectClass) {
		OPCPackage pkg = null;
		ExcelWorkSheetHandler<T> workSheetHandler = new ExcelWorkSheetHandler<T>(objectClass,
				cellMapping);
		
		ByteArrayInputStream in = new  ByteArrayInputStream(file);
		//InputStream in = Excel2Java.class.getClassLoader().getResources(file);
		try {
			pkg = OPCPackage.open(in);
		} catch (InvalidFormatException ife) {
			LOG.error(ife.getMessage(), ife.getCause());
		} catch (IOException ioe) {
			LOG.error(ioe.getMessage(), ioe.getCause());
		} finally {
			IOUtils.closeQuietly(in);
			try {
				if (null != pkg) {
					pkg.close();
				}
			} catch (IOException e) {
				// just ignore IO exception
			}
		}
		ExcelReader excelReader = new ExcelReader(pkg, workSheetHandler);
		excelReader.process();

		if (workSheetHandler.getValueList().isEmpty()) {
			// No data present
			LOG.error("sHandler.getValueList() is empty");
		} else {

			LOG.info(workSheetHandler.getValueList().size()
					+ " no. of records read from given excel worksheet successfully.");

			// Displaying data ead from Excel file
			return workSheetHandler.getValueList();

		}
		return null;
	}	
}
